
from persistent import Persistent
from BTrees.OOBTree import OOTreeSet
from zope.component import getSiteManager, getUtility
from zope.interface import implements
from zope.app.container.btree import BTreeContainer
from translation import TranslationGroup
from interfaces import ITranslationUtility
from Products.CMFCore.utils import getToolByName
from plone.i18n.locales import languages
import random
import Acquisition

def add_multilingual(context):
    sm = getSiteManager(context) 
    if sm.queryUtility(ITranslationUtility,
                       name=u'TranslationUtility',
                       default=None) is None:
        obj_translation_utility = TranslationUtility()
        sm.registerUtility(obj_translation_utility, ITranslationUtility,  name=u'TranslationUtility')


class TranslationUtility(BTreeContainer):
    """ Translation Utility
    """
    implements(ITranslationUtility)
    
    """ Group of translations """

    def _generate_id(self, object):
        return ''.join(random.sample(
            "abcdefghijklmnopqrtstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-1234567890",
            30))

    def addTranslationGroup(self, object, language=None):
        """ Generate a random key to store the TranslationGroup"""
        key = self._generate_id(object)
        while key in self._SampleContainer__data:
            key = self._generate_id(object)
        self[key]=TranslationGroup()
        if language==None or language=='':
            language=getToolByName(object,'portal_languages').getDefaultLanguage()
            object.setLanguage(language)
            object.reindexObject()
        self[key].addTranslation(language=language,context=object, translatedObject=object)
        self[key].setAuthoritative(language)
        return self[key]

    def getGroupId(self, context):
        """ Get the id of the translation group """
        pass

